import cv2
import numpy as np

from tool.MyTool import getRandomInt


def crcb_range_sceening(img):
    ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
    (y, cr, cb) = cv2.split(ycrcb)

    skin = np.zeros(cr.shape, dtype=np.uint8)
    (x, y) = cr.shape
    for i in range(0, x):
        for j in range(0, y):
            if (cr[i][j] > 140) and (cr[i][j]) < 175 and (cr[i][j] > 100) and (cb[i][j]) < 120:
                skin[i][j] = 255
            else:
                skin[i][j] = 0

    dst = cv2.bitwise_and(img, img, mask=skin)

    return dst


def ellipse_detect(img):
    skinCrCbHist = np.zeros((256, 256), dtype=np.uint8)
    cv2.ellipse(skinCrCbHist, (113, 155), (23, 15), 43, 0, 360, (255, 255, 255), -1)

    YCRCB = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
    (y, cr, cb) = cv2.split(YCRCB)
    skin = np.zeros(cr.shape, dtype=np.uint8)
    (x, y) = cr.shape
    for i in range(0, x):
        for j in range(0, y):
            CR = YCRCB[i, j, 1]
            CB = YCRCB[i, j, 2]
            if skinCrCbHist[CR, CB] > 0:
                skin[i, j] = 255

    # cv2.namedWindow(image, cv2.WINDOW_NORMAL)
    # cv2.imshow(image, img)
    dst = cv2.bitwise_and(img, img, mask=skin)
    return dst


def getSkinByCvcr(inputFrame):
    '''
    通过YUV 色域分割
    :param inputFrame:
    :return:
    '''
    ycrcb = cv2.cvtColor(inputFrame, cv2.COLOR_BGR2YCrCb)  # 把图像转换到YUV色域
    (y, cr, cb) = cv2.split(ycrcb)  # 图像分割, 分别获取y, cr, br通道图像
    # 高斯滤波, cr 是待滤波的源图像数据, (5,5)是值窗口大小, 0 是指根据窗口大小来计算高斯函数标准差
    cr1 = cv2.GaussianBlur(cr, (5, 5), 0)  # 对cr通道分量进行高斯滤波
    # 根据OTSU算法求图像阈值, 对图像进行二值化
    _, skin1 = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    return skin1


def createHogVectorFromImg(img):
    winSize = (240, 240)
    blockSize = (120, 120)
    blockStride = (60, 60)
    cellSize = (60, 60)
    nbins = 9

    # img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY,dstCn=img.shape[2])
    # ret, img = cv2.threshold(img, 100, 120, cv2.THRESH_OTSU)

    hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins)
    img = cv2.resize(img, (240, 240))
    vec = hog.compute(img=img, winStride=(120, 120), padding=(0, 0))
    DIMEN = len(vec)
    vec = vec.astype(np.float32).reshape((1, DIMEN))
    return vec



if __name__ == '__main__':



    cap = cv2.VideoCapture(0)
    while 1:
        ret, frame = cap.read()
        cv2.flip(frame, 1, frame)
        # cv2.imshow("src", frame)
        # srcClone = frame.copy()

        frame = getSkinByCvcr(frame)

        source_height = frame.shape[0]
        source_width = frame.shape[1]

        # print("swh:", source_width, source_height)  # 720,1280

        contours, hierarchy = cv2.findContours(frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

        max_width = 100
        max_height = 140

        for i in range(len(contours)):
            rect = cv2.boundingRect(contours[i])  # x，y，w，h
            x = rect[0]
            y = rect[1]
            width = rect[2]
            height = rect[3]
            if width > max_width and height > max_height < height:
                target = frame[y:y + height, x:x + width]
                cv2.imwrite(str(getRandomInt()) + ".png", target)



        if cv2.waitKey(10) == 27:
            break
        else:
            cv2.imshow("result", frame)

    cap.release()
    cv2.destroyAllWindows()
